package com.ruoyi.web.controller.danger;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.domain.vo.PreventionCheckTaskConfigCountVO;
import com.ruoyi.system.domain.vo.PreventionResultVO;
import com.ruoyi.system.entity.AlarmPerimeterRecord;
import com.ruoyi.system.mapper.AlarmPerimeterRecordMapper;
import com.ruoyi.system.mapper.SysFirePointMapper;
import com.ruoyi.system.service.AlarmPerimeterRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (电子围栏报警)表控制层
 */
@Slf4j
@RestController
@RequestMapping("alarmPerimeterRecord")
public class AlarmPerimeterRecordController extends BaseController {

    @Resource
    private AlarmPerimeterRecordService alarmPerimeterRecordService;
    @Resource
    private AlarmPerimeterRecordMapper alarmPerimeterRecordDao;
    @Resource
    private SysFirePointMapper sysFirePointMapper;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @GetMapping("/list")
    public TableDataInfo queryByPage(AlarmPerimeterRecord alarmPerimeterRecord) {
        startPage();
        Page<Object> localPage = PageUtils.getLocalPage();
        int pageNum = localPage.getPageNum();
        int pageSize = localPage.getPageSize();
        String sendGet = HttpUtils.sendGet("http://192.168.18.158:39097/jdbc/alarmPerimeterRecord/list?pageSize=" + pageSize + "&pageNumber=" +  (pageNum - 1) * pageSize, null);
        log.info("==============电子围栏报警==========={}", JSON.toJSONString(sendGet));
        return JSON.parseObject(sendGet, TableDataInfo.class);
    }

    /**
     * 通过主键查询单条数据
     *
     * @return 单条数据
     */
    @GetMapping("{sn}")
    public AjaxResult queryById(@PathVariable("sn") String sn) {
        return AjaxResult.success(this.alarmPerimeterRecordService.queryById(sn));
    }

    /**
     * 新增数据
     *
     * @param alarmPerimeterRecord 实体
     * @return 新增结果
     */
    @PostMapping("/create")
    public AjaxResult add(AlarmPerimeterRecord alarmPerimeterRecord) {
        return AjaxResult.success(this.alarmPerimeterRecordService.insert(alarmPerimeterRecord));
    }

    /**
     * 编辑数据
     *
     * @param alarmPerimeterRecord 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    public AjaxResult edit(AlarmPerimeterRecord alarmPerimeterRecord) {
        return AjaxResult.success(this.alarmPerimeterRecordService.update(alarmPerimeterRecord));
    }

    /**
     * 删除数据
     *
     * @return 删除是否成功
     */
    @GetMapping("/delete")
    public AjaxResult deleteById(String sn) {
        return AjaxResult.success(this.alarmPerimeterRecordService.deleteById(sn));
    }


    /**
     * 统计_电子围栏/_安全仪表
     *
     * @param startTime alarmPerimeterRecord
     * @param endTime
     * @return
     */
    @GetMapping("/countAlarmByLevel")
    public AjaxResult countAlarmByLevel(String startTime, String endTime) {
        PreventionResultVO vo = new PreventionResultVO();
        List<PreventionCheckTaskConfigCountVO> alarmList = alarmPerimeterRecordDao.queryCountAlarm(startTime, endTime);
        List<PreventionCheckTaskConfigCountVO> sysFirePointList = sysFirePointMapper.queryCountSysFirePoint(startTime, endTime);
        vo.setAlarmList(alarmList);
        vo.setSysFirePointList(sysFirePointList);
        return AjaxResult.success(vo);
    }


}

